package com.huanzhidadi.single.completablefuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 继续执行任务，无返回值
 * thenRun 使用的是执行上一个任务的线程池
 * thenRunAsync 不指定线程池时使用的是ForkJoin线程池
 * CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor)
 */
public class ThenRunTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程：" + Thread.currentThread().getName());
        }, executorService).thenRunAsync(() -> {
            System.out.println("后续子线程：" + Thread.currentThread().getName());
            executorService.shutdown();
        },executorService);

        System.out.println("主线程结束："+Thread.currentThread().getName());
    }
}
